package com.weirong.smartvault.backservice.util.table;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/29
 * Time: 11:44
 */
public class TableRow<T> {
    List<TableCell<T>> cells = new ArrayList<>();
    Integer y = 0;

    public TableCell<T> get(Integer i){
        return cells.get(i);
    }

    public Integer getSize(){
        return cells.size();
    }
    public Integer size(){
        return cells.size();
    }
    public Integer getY(){
        return y;
    }
    public void addNewCell(String content,Integer height){
        cells.add(new TableCell<>(content, height));
    }
    public Integer Increment(Integer height){//需要增加的高度
        if (y+height>cells.size())
            return y+height-cells.size();
        return 0;
    }
    public void setCell(String content,Integer height,T obj){
        setCell(content,height,obj,"");
    }

    public void setCell(String content, Integer height, T obj, String additionInf) {
        TableCell<T> cell = cells.get(y);
        cell.setAvailable(true);
        cell.setContent(content);
        cell.setHeight(height);
        cell.setObj(obj);
        cell.additionInf = additionInf;
        for (int i = 1; i < height; i++) {
            TableCell affect= cells.get(i+y);
            affect.setHeight(0);
            affect.setAvailable(true);
        }
        y += height;
    }
    public void setCell(String content,Integer height){
        setCell(content,height,null);
    }

    public void freeze() {
        TableCell last = null;
        for(TableCell cell:cells){
            if(!cell.getAvailable()){
                if (last==null)
                    last = cell;
                else{
                    last.setHeight(last.getHeight()+1);
                    cell.setHeight(0);
                }
                cell.setAvailable(true);
            }else
                last = null;
        }
        y = cells.size();
    }
    public Integer getUncheckCountAndSet(){
        Integer count = 0 ;
        for(TableCell cell:cells){
            if(!cell.getChecked()){
                count+=1;
                cell.setChecked(true);
            }
        }
        return count;
    }

}
